<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-item label="观物号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['guanwuNo', validatorRules.guanwuNo]" placeholder="请输入观物号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="名字" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入名字"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="头衔" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['title', validatorRules.title]" placeholder="请输入头衔"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="统一id" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['pin', validatorRules.pin]" placeholder="请输入统一id"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="手机" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['phone', validatorRules.phone]" placeholder="请输入手机"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="密码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-password v-decorator="['password', validatorRules.password]" placeholder="请输入密码" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="slat" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['salt', validatorRules.salt]" placeholder="请输入slat"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="头像" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['pic']" placeholder="请输入头像"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="签名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['sign']" placeholder="请输入签名"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="二维码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['qrCode']" placeholder="请输入二维码"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="wechatId" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['wechat']" placeholder="请输入wechatId"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['sex']" :trigger-change="true" dictCode="sex" placeholder="请选择性别" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['email', validatorRules.email]" placeholder="请输入邮箱"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['lng']" placeholder="请输入经度"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['lat']" placeholder="请输入纬度"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="位置名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['locationTitle']" placeholder="请输入位置名称"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="年龄" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['age', validatorRules.age]" placeholder="请输入年龄"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="请求数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['friendCount']" placeholder="请输入请求数"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="被请求数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['friendedCount']" placeholder="请输入被请求数"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="cpm" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['cpm']" placeholder="请输入cpm"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="cpc" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['cpc']" placeholder="请输入cpc"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="成交数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dealCount']" placeholder="请输入成交数"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="转发数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['transfferCount']" placeholder="请输入转发数"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="被转发数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['transfferedCount']" placeholder="请输入被转发数"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="结算数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dealedCount']" placeholder="请输入结算数"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="订单数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['orderingCount']" placeholder="请输入订单数"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep v-decorator="['createBy']" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择创建日期" v-decorator="['createTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="更新人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep v-decorator="['updateBy']" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="更新日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择更新日期" v-decorator="['updateTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'GuanwuProfileForm',
    components: {
      JFormContainer,
      JDate,
      JSelectUserByDep,
      JDictSelectTag,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          guanwuNo: {
            rules: [
              { required: true, message: '请输入观物号!'},
            ]
          },
          name: {
            rules: [
              { required: true, message: '请输入名字!'},
            ]
          },
          title: {
            rules: [
              { required: true, message: '请输入头衔!'},
            ]
          },
          pin: {
            rules: [
              { required: true, message: '请输入统一id!'},
            ]
          },
          phone: {
            rules: [
              { required: true, message: '请输入手机!'},
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
            ]
          },
          password: {
            rules: [
              { required: true, message: '请输入密码!'},
            ]
          },
          salt: {
            rules: [
              { required: true, message: '请输入slat!'},
            ]
          },
          email: {
            rules: [
              { required: false},
              { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},
            ]
          },
          age: {
            rules: [
              { required: false},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
        },
        url: {
          add: "/profile/profile/guanwuProfile/add",
          edit: "/profile/profile/guanwuProfile/edit",
          queryById: "/profile/profile/guanwuProfile/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'guanwuNo','name','title','pin','phone','password','salt','pic','sign','qrCode','wechat','sex','email','lng','lat','locationTitle','age','friendCount','friendedCount','cpm','cpc','dealCount','transfferCount','transfferedCount','dealedCount','orderingCount','createBy','createTime','updateBy','updateTime'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'guanwuNo','name','title','pin','phone','password','salt','pic','sign','qrCode','wechat','sex','email','lng','lat','locationTitle','age','friendCount','friendedCount','cpm','cpc','dealCount','transfferCount','transfferedCount','dealedCount','orderingCount','createBy','createTime','updateBy','updateTime'))
      },
    }
  }
</script>